package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.FriendDto;
import com.lanjiu.lib.imclient.dto.GroupDto;
import com.lanjiu.lib.imclient.dto.SettingsDto;
import com.lanjiu.lib.imclient.dto.VersionDto;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.imclient.dto.notify.SysNotifyDto;
import com.lanjiu.lib.imclient.dto.user.RegisteredUserDto;
import com.lanjiu.lib.imclient.dto.user.UserDto;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.ims.offline.OfflineTimeoutRunnable;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.ParseUtils;
import com.lanjiu.lib.utils.json.JsonUtils;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.activity.VersionInfoActivity;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.dao.UserTableBean;
import com.ljkj.cordial.chat.ui.IView;

import java.util.List;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/11/20 13:49
 */
public class ChatMainView extends IView {

    private ChatMainCallback mCallback;
    private int mFriendPageIndex;
    private int mGroupPageIndex;
    private OfflineTimeoutRunnable mFriendOfflineTimeoutDelayed;
    private OfflineTimeoutRunnable mGroupOfflineTimeoutDelayed;

    public ChatMainView(ChatMainCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doGetFriendList() {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.FRIEND_LIST_REGISTERED : EventConstants.Action.FRIEND_LIST_TOURISTS;
        doSendRequestOnBackground(action);
    }

    public void doGetGroupList() {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_LIST_REGISTERED : EventConstants.Action.GROUP_LIST_TOURISTS;
        doSendRequestOnBackground(action);
    }

    public void doGetSysNotifyList() {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.NOTIFICATIONS_REGISTERED : EventConstants.Action.NOTIFICATIONS_TOURISTS;
        doSendRequestOnBackground(action);
    }

    public void doLogin() {
        UserTableBean userTableBean = ChatApp.getInstance().get();
        if (userTableBean != null) {
            RegisteredUserDto registeredUser = new RegisteredUserDto();
            registeredUser.setUserId(userTableBean.getUserId());
            registeredUser.setSecret(userTableBean.getSecret());
            doSendRequestOnBackground(EventConstants.Action.LOGIN_REGISTERED, registeredUser.toJson());
        }
    }

    public void doLogout() {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.LOGOUT_REGISTERED : EventConstants.Action.LOGOUT_TOURISTS;
        doSendRequest(action);
    }

    public void doGetOffMsgList() {
        if (mFriendPageIndex == 0)
            doGetFriendOfflineMsgList(1);
        if (mGroupPageIndex == 0)
            doGetGroupOfflineMsgList(1);
    }

    public void doGetFriendOfflineMsgList(int page) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.OFF_MESSAGE_SEND_REGIST : EventConstants.Action.OFF_MESSAGE_SEND_TOURIST;
        doSendRequestOnBackground(action, String.valueOf(page));
    }

    public boolean hasFriendOfflineTimeoutDelayed() {
        return mFriendOfflineTimeoutDelayed != null;
    }

    public Runnable getFriendOfflineTimeoutDelayed() {
        return mFriendOfflineTimeoutDelayed;
    }

    public Runnable getFriendOfflineTimeoutDelayed(int page) {
        if (mFriendOfflineTimeoutDelayed == null) {
            mFriendOfflineTimeoutDelayed = new OfflineTimeoutRunnable(page) {
                @Override
                public void onTimeoutRun(int page) {
                    doGetFriendOfflineMsgList(page);
                }
            };
        } else {
            mFriendOfflineTimeoutDelayed.setPage(page);
        }
        return mFriendOfflineTimeoutDelayed;
    }

    public void doGetGroupOfflineMsgList(int page) {
        doSendRequestOnBackground(EventConstants.Action.GROUP_CHART_OFF_LINE_MSG, String.valueOf(page));
    }

    public boolean hasGroupOfflineTimeoutDelayed() {
        return mGroupOfflineTimeoutDelayed != null;
    }

    public Runnable getGroupOfflineTimeoutDelayed() {
        return mGroupOfflineTimeoutDelayed;
    }

    public Runnable getGroupOfflineTimeoutDelayed(int page) {
        if (mGroupOfflineTimeoutDelayed == null) {
            mGroupOfflineTimeoutDelayed = new OfflineTimeoutRunnable(page) {
                @Override
                public void onTimeoutRun(int page) {
                    doGetGroupOfflineMsgList(page);
                }
            };
        } else {
            mGroupOfflineTimeoutDelayed.setPage(page);
        }
        return mGroupOfflineTimeoutDelayed;
    }

    public void doCheckUpdate() {
        doSendRequest(EventConstants.Action.CHECK_FOR_UPDATES, new VersionDto().toJson());
    }

    public void doGetSettingsInfo() {
        doSendRequest(EventConstants.Action.PERSONAL_SETTINGS_INFO);
    }

    @Override
    public void onMessageEventHandle(MessageEvent event) {
        switch (event.action()) {
            case EventConstants.Action.FRIEND_LIST_TOURISTS:
            case EventConstants.Action.FRIEND_LIST_REGISTERED:
                Logger.print("ChatMainView", event.toString());
                List<FriendDto> friendList = null;
                if (checkIsSuccess(event.code(), event.data()))
                    friendList = JsonUtils.convertJsonToList(event.data(), FriendDto.class);
                if (null != mCallback)
                    mCallback.onFriendListCallback(event.code(), event.msg(), friendList);
                break;
            case EventConstants.Action.GROUP_LIST_TOURISTS:
            case EventConstants.Action.GROUP_LIST_REGISTERED:
                Logger.print("ChatMainView", event.toString());
                List<GroupDto> groupList = null;
                if (checkIsSuccess(event.code(), event.data()))
                    groupList = JsonUtils.convertJsonToList(event.data(), GroupDto.class);
                if (null != mCallback)
                    mCallback.onGroupListCallback(event.code(), event.msg(), groupList);
                break;
            case EventConstants.Action.OFF_MESSAGE_SEND_TOURIST:
            case EventConstants.Action.OFF_MESSAGE_SEND_REGIST:
                Logger.print("ChatMainView", event.toString());
                boolean fRequest = false;
                if (checkIsSuccess(event.code(), event.data())) {
                    String[] split = event.data().split(",");
                    if (split.length == 2) {
                        mFriendPageIndex = ParseUtils.parseInt(split[0]);
                        fRequest = ParseUtils.parseBoolean(split[1]);
                        if (!fRequest)
                            mFriendPageIndex = 0;
                    }
                }
                if (event.code() != IMSContacts.ResponseCode.TIMEOUT && event.code() != IMSContacts.ResponseCode.SUCCESS) {
                    mFriendPageIndex = 0;
                }
                if (null != mCallback)
                    mCallback.onGetFriendOffMsgListCallback(event.code(), event.msg(), mFriendPageIndex, fRequest);
                break;
            case EventConstants.Action.GROUP_CHART_OFF_LINE_MSG:
                Logger.print("ChatMainView", event.toString());
                boolean gRequest = false;
                if (checkIsSuccess(event.code(), event.data())) {
                    String[] split = event.data().split(",");
                    if (split.length == 2) {
                        mGroupPageIndex = ParseUtils.parseInt(split[0]);
                        gRequest = ParseUtils.parseBoolean(split[1]);
                        if (!gRequest)
                            mGroupPageIndex = 0;
                    }
                }
                if (event.code() != IMSContacts.ResponseCode.TIMEOUT && event.code() != IMSContacts.ResponseCode.SUCCESS) {
                    mGroupPageIndex = 0;
                }
                if (null != mCallback)
                    mCallback.onGetGroupOffMsgListCallback(event.code(), event.msg(), mGroupPageIndex, gRequest);
                break;
            case EventConstants.Action.LOGOUT_TOURISTS:
            case EventConstants.Action.LOGOUT_REGISTERED:
                Logger.print("ChatMainView", event.toString());
                if (null != mCallback)
                    mCallback.onLogoutCallback(event.code(), event.msg());
                break;
            case EventConstants.Action.LOGIN_REGISTERED:
                UserDto user = null;
                if (checkIsSuccess(event.code(), event.data())) {
                    RegisteredUserDto registeredUser = Dto.fromJson(event.data(), RegisteredUserDto.class);
                    if (null != registeredUser) user = new UserDto(registeredUser);
                }
                if (null != mCallback)
                    mCallback.onLoginCallback(event.code(), event.msg(), user);
                break;
            case EventConstants.Action.NOTIFICATIONS_TOURISTS:
            case EventConstants.Action.NOTIFICATIONS_REGISTERED:
                Logger.print("ChatMainView", event.toString());
                List<SysNotifyDto> sysNotifyList = null;
                if (checkIsSuccess(event.code(), event.data())) {
                    sysNotifyList = JsonUtils.convertJsonToList(event.data(), SysNotifyDto.class);
                }
                if (null != mCallback)
                    mCallback.onGetNotifyCallback(event.code(), event.msg(), sysNotifyList);
                break;
            case EventConstants.Action.CHECK_FOR_UPDATES:
                Logger.print("ChatMainView", event.toString());
                if (!VersionInfoActivity.IS_RUNNING) {
                    VersionDto version = null;
                    if (checkIsSuccess(event.code(), event.data()))
                        version = Dto.fromJson(event.data(), VersionDto.class);
                    if (mCallback != null)
                        mCallback.onCheckVersionCallback(event.code(), event.msg(), version);
                }
                break;
            case EventConstants.Action.PERSONAL_SETTINGS_INFO:
                SettingsDto settingsDto = null;
                if (checkIsSuccess(event.code(), event.data()))
                    settingsDto = Dto.fromJson(event.data(), SettingsDto.class);
                if (null != mCallback)
                    mCallback.onSettingsInfoCallback(event.code(), event.msg(), settingsDto);
                break;
        }
    }

    public interface ChatMainCallback {

        void onFriendListCallback(int code, String msg, List<FriendDto> data);

        void onGroupListCallback(int code, String msg, List<GroupDto> data);

        void onGetFriendOffMsgListCallback(int code, String msg, int page, boolean request);

        void onGetGroupOffMsgListCallback(int code, String msg, int page, boolean request);

        void onLogoutCallback(int code, String msg);

        void onLoginCallback(int code, String msg, UserDto data);

        void onGetNotifyCallback(int code, String msg, List<SysNotifyDto> data);

        void onCheckVersionCallback(int code, String msg, VersionDto data);

        void onSettingsInfoCallback(int code, String msg, SettingsDto data);
    }
}
